home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 112 / EnigmaAmiga112CD.iso / dalla rivista / news / orbit / source / lights.c < prev    next >
C/C++ Source or Header  |  2000-05-01  |  4KB  |  159 lines

  1. /*
  2.     Amiga port by Oliver Gantert
  3.  
  4.     27.04.2000 - fixed some compiler warnings
  5. */
  6. /*
  7.  
  8. ORBIT, a freeware space combat simulator
  9. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  
  25. */
  26.  
  27. #include "orbit.h"
  28.  
  29. static float ambient[]         =   {
  30.   0.0, 0.0, 0.0, 1.0 }
  31. ;
  32. static float diffuse[]         =   {
  33.   1.0, 1.0, 0.0, 1.0 }
  34. ;
  35. static float position0[]       =   {
  36.   0.0, 0.0, 0.0, 1.0 }
  37. ;
  38. static float lmodel_ambient[]  =   {
  39.   0.01, 0.01, 0.01, 1.0 }
  40. ;
  41. static float lmodel_twoside[]  =   {
  42.   GL_TRUE}
  43. ;
  44. static float front_shininess[] =   {
  45.   50.0}
  46. ;
  47. static float front_specular[]  =   {
  48.   1.0, 1.0, 1.0, 1.0 }
  49. /* static float front_specular[]  =   { 0.0, 0.7, 0.0, 1.0 }; */
  50.  
  51. void Lights()
  52. {
  53.   int l;
  54.   double v[3];
  55.   float f[3];
  56.  
  57.   /* Enable depth buffer (why is this *here*?) */
  58.   glDepthFunc (GL_LEQUAL);
  59.   glEnable (GL_DEPTH_TEST);
  60.  
  61.   /* Turn off ambient light */
  62.   glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  63.   glLightfv (GL_LIGHT0, GL_AMBIENT, lmodel_ambient);
  64.  
  65.   /* Define light0, the big, main light */
  66.   /* glLightfv (GL_LIGHT0, GL_POSITION, position0); */
  67.   Vsub (v, planet[0].pos, player.pos);
  68.   f[0] = (float) v[0];
  69.   f[1] = (float) v[1];
  70.   f[2] = (float) v[2];
  71.   glLightfv (GL_LIGHT0, GL_POSITION, f);
  72.  
  73.   /* Turn on lighting in general */
  74.   glEnable (GL_LIGHTING);
  75.  
  76.   /* Turn on individual lights */
  77.   glEnable (GL_LIGHT0);
  78.  
  79.   /* Check each non-permanent light */
  80.   for (l=0; l<NLIGHTS; l++)
  81.   {
  82.     if (light[l].age > 0.0)
  83.     {
  84.       light[l].age += deltaT;
  85.       glLightfv (light[l].gl_num, GL_DIFFUSE, light[l].color);
  86.       glLightfv (light[l].gl_num, GL_SPECULAR, light[l].color);
  87.       /*   glLightfv (light[l].gl_num, GL_POSITION, light[l].pos); */
  88.       f[0] = light[l].pos[0] - (float) player.pos[0];
  89.       f[1] = light[l].pos[1] - (float) player.pos[1];
  90.       f[2] = light[l].pos[2] - (float) player.pos[2];
  91.       glLightfv (light[l].gl_num, GL_POSITION, f);
  92.       glEnable (light[l].gl_num);
  93.     }
  94.     else
  95.     {
  96.       /*   glDisable (light[l].gl_num); */
  97.     }
  98.   }
  99.  
  100.   /* Pick smooth or flat shading */
  101.   glShadeModel (GL_SMOOTH);
  102.   /* glShadeModel (GL_FLAT);  */
  103. }
  104.  
  105. void InitLights()
  106. /*
  107.  *  Set up non-permanent light sources
  108.  */
  109. {
  110.   int i;
  111.  
  112.   for (i=0; i<NLIGHTS; i++)
  113.   {
  114.     light[i].age = 0.0;
  115.     light[i].pos[3] = 1.0;
  116.     light[i].gl_num = GL_LIGHT1 + i;
  117.   }
  118. }
  119.  
  120. int FindLight()
  121. /*
  122.  *  Return free or oldest light
  123.  */
  124. {
  125.   int i, oldest;
  126.   double maxage;
  127.  
  128.   for (i=0; i<NLIGHTS; i++)
  129.   {
  130.     if (light[i].age == 0.0) return (i);
  131.  
  132.     if (i == 0)
  133.     {
  134.       oldest = i;
  135.       maxage = light[i].age;
  136.     }
  137.     else
  138.     {
  139.       if (light[i].age > maxage)
  140.       {
  141.         maxage = light[i].age;
  142.         oldest = i;
  143.       }
  144.     }
  145.   }
  146.  
  147.   return (oldest);
  148. }
  149.  
  150. void DestroyLight (int l)
  151. /*
  152.  *  Destroy specified light
  153.  */
  154. {
  155.   light[l].age = 0.0;
  156.   glDisable (light[l].gl_num);
  157. }
  158.